package com.digital.herbarium.service;

import com.digital.herbarium.Exception.CustomException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

@Service
public class ZipService {

    @Value("${static.url}")
    private String IMAGES_DIR;

    public void unzipImages(String zipFilename) {
        try {
            System.out.println("****开始解压"+zipFilename);
            ZipFile zipFile = new ZipFile(zipFilename);
            zipFile.stream().forEach(entry -> {
                if (!entry.isDirectory()) {
                    String entryName = entry.getName();

                    System.out.println("文件名称；"+entryName);
                    System.out.println("IMAGES_DIR***:"+IMAGES_DIR);
                    File file = new File(IMAGES_DIR +File.separator+ entryName);
                    try {
                        InputStream inputStream = zipFile.getInputStream(entry);
                        System.out.println("****进来了");
                        OutputStream outputStream = new FileOutputStream(file);
                        System.out.println("***这里");
                        byte[] buffer = new byte[1024];
                        int len;
                        while ((len = inputStream.read(buffer)) > 0) {
                            outputStream.write(buffer, 0, len);
                        }
                        inputStream.close();
                        outputStream.close();
                    } catch (Exception e) {
                        // 处理异常
                        throw new CustomException(400,e.toString());
                    }
                }
            });
            zipFile.close();
        } catch (Exception e) {
            // 处理异常
            throw new CustomException(400,e.toString());
        }
    }

    public boolean unzip(String srcZipFile) {
        boolean isSuccessful = true;
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcZipFile));
            ZipInputStream zis = new ZipInputStream(bis);

            BufferedOutputStream bos = null;

            //byte[] b = new byte[1024];
            ZipEntry entry = null;
            while ((entry=zis.getNextEntry()) != null) {
                String entryName = entry.getName();
                bos = new BufferedOutputStream(new FileOutputStream(IMAGES_DIR + File.separator + entryName));
                int b = 0;
                while ((b = zis.read()) != -1) {
                    bos.write(b);
                }
                bos.flush();
                bos.close();
            }
            zis.close();
        } catch (IOException e) {
            isSuccessful = false;
        }
        return isSuccessful;
    }


    public void getAllFile(File dirPath,List<File>  allFileList) {

        File[] fileList = dirPath.listFiles();
        System.out.println("fileList ***+"+fileList);
        assert fileList != null;
        for (File file : fileList) {
            if (file.isDirectory()) {
                getAllFile(file,allFileList);
            } else {
                allFileList.add(file);
            }
        }
    }


    public  void deleteFileByIO(String filePath) {
        File file = new File(filePath);
        File[] list = file.listFiles();
        if (list != null) {
            for (File temp : list) {
                deleteFileByIO(temp.getAbsolutePath());
            }
        }
        file.delete();
    }
}
